<?php

abstract class Controller extends QXC
{	
    public $ModelName;

    function __construct()
    {

    }

    function __get($name)
    {
        if (isset($name) && ctype_alnum($name))
        {
            switch (strtolower($name))
            {
                case 'model' :
                    $this->$name = $this->loadModel();
                    break;
                case 'view' :
                    $this->$name = $this->loadView();
                    break;
                default :
                    $this->$name = $this->loadModule($name);
                    break;
            }

            return $this->$name;
        }
    }

    /**
     * @return View
     */
    public function View($name = '')
    {
        if($this->View == null)
        {
            $this->View = $this->loadView($name);
        }

        $this->View->SetName($name);

        return $this->View;
    }

    /**
     * @return Model
     */
    public function Model($name = '')
    {
        $this->Model = $this->loadModel($name);
        
        return $this->Model;
    }

    private function loadView($name = '')
    {
        return View::Load($name);
    }

    private function loadModel($name = '')
    {
        $modelName = (empty($name) ? (empty($this->ModelName) ? get_class($this) : $this->ModelName) : $name);

        return Model::Load($modelName);
    }

    private function loadModule($name = '')
    {
        $mName = (empty($name) ? (empty($this->ModuleName) ? get_class($this) : $this->ModuleName) : $name);

        if($module = Module::Load($mName))
            return $module;
        else
            return parent::loadExtension($name);
    }

    public static function Load($args, $path)
    {
        $source = $args;
        $className = array_shift($args);
        $methodName = array_shift($args);

        $className = (is_null($className) || empty ($className)) ? CORE_MAIN_CONTROLLER : strtolower(trim($className));
        $methodName = (is_null($methodName) || empty ($methodName)) ? CORE_MAIN_CONTROLLER : strtolower(trim($methodName));

        if($contPath = QXC::pathExist($path, "{$className}." . CORE_PHP_EXT))
        {

        }
        else if($contPath = QXC::pathExist($path, CORE_MAIN_CONTROLLER . "." . CORE_PHP_EXT))
        {
            $methodName = $className;
            $className = CORE_MAIN_CONTROLLER;

            $args = array_slice($source, 1);
        }
        else
        {
            return false;
        }

        // Include controller file
        include_once ($contPath);

        $cntClassName = "{$className}Controller";
        $viewName = ($className == CORE_MAIN_CONTROLLER ? "" : "{$className}/") . "{$methodName}";
        $modelName = $className;

        $cntObject = new $cntClassName($className, $methodName, $viewName, $modelName);

        if(is_callable(array($cntObject, $methodName)))
        {
            if(!$cntObject->View->GetName())
            {
            // Automatically set View name
                $cntObject->View->SetName($viewName);
            }

            if(empty ($cntObject->ModelName))
            {
                $cntObject->ModelName = $modelName;
            }

            $result = call_user_func_array(array($cntObject, $methodName), $args);

            // Render final View
            if($result !== false)
            {
                $cntObject->View->Render();
            }

            return true;
        }
        // TODO: Work on this
//        else
//        {
//            throw new QWebException("404 Method Not Found",
//            "<strong>/controllers/{$className}::{$methodName}</strong> - method doesn't exists!");
//        }
    }
}